package com.isay.app.honey.ui.splash;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

import com.isay.app.honey.R;
import com.isay.app.honey.manager.NetworkManager;
import com.isay.app.honey.ui.login.LoginActivity;
import com.isay.app.honey.ui.main.MainActivity;
import com.isay.frameworklib.okhttp.ICommonCallback;
import com.isay.frameworklib.ui.BaseActivity;
import com.isay.frameworklib.user.UserManager;
import com.isay.frameworklib.utils.PreferencesUtils;
import com.isay.frameworklib.utils.WebUrlUtils;

import org.json.JSONObject;


public class SplashActivity extends BaseActivity implements View.OnClickListener {

    private final static String KEY_SPLASH = "key_splash";
    private String mUrlSplash;
    private TextView mProgressBar;
    private int mCurProgress;

    @Override
    protected void onCreateActivity(Bundle savedInstanceState) {
        setContentView(R.layout.activity_splash);
        findViewById(R.id.tv_skip).setOnClickListener(this);
        findViewById(R.id.lay_splasy).setOnClickListener(this);
        mProgressBar = findViewById(R.id.progressBar);
        mUrlSplash = PreferencesUtils.getString(KEY_SPLASH, "");
        getSplash();
        delaySecond(2900);
    }


    @Override
    protected int getStatusBarColor() {
        return 0;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_splasy:
                WebUrlUtils.openUrl(SplashActivity.this, mUrlSplash);
                break;
            case R.id.tv_skip:
               /* String username = UserManager.getUserName();
                String password = UserManager.getUserPwd();
                if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
                    login(username, password);
                } else {
                    gotoMainActivity(false);
                }*/
                break;
            default:
                break;
        }
    }


    private void clickLogin() {
        String username = UserManager.getUserName();
        String password = UserManager.getUserPwd();
        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
            mProgressBar.setText("登录中...");
            login(username, password);
        } else {
            gotoMainActivity(false);
        }
    }

    private void login(final String username, final String password) {
        NetworkManager.login(username, password, new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                if (LoginActivity.loginSuccess(ret, msg, content)) {
                    gotoMainActivity(true);
                } else {
                    gotoMainActivity(false);
                }
            }

            @Override
            public void onFailure(Exception e) {
                gotoMainActivity(false);
            }
        });
    }


    /**
     * 延时智学
     */
    private void delaySecond(int duration) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);
        valueAnimator.setDuration(duration);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int currentValue = (Integer) animation.getAnimatedValue();
                int index = 3;
                if (currentValue > 66) {
                    index = 1;
                } else if (currentValue > 33) {
                    index = 2;
                } else {
                    index = 3;
                }
                if (index != mCurProgress) {
                    mCurProgress = index;
                    mProgressBar.setText(String.valueOf(index) +" s");
                }
                if (currentValue >= 100) {
                    clickLogin();
                }
            }
        });
        valueAnimator.start();
    }


    private void gotoMainActivity(boolean loginSuccess) {
        if (loginSuccess) {
            MainActivity.launch(SplashActivity.this);
            finish();
        } else {
            LoginActivity.launch(SplashActivity.this);
            finish();
        }
    }


    //获取启动页信息
    private void getSplash() {
        NetworkManager.splash(new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                if (ret == 200) {
                    try {
                        JSONObject jsonObject = new JSONObject(content);
                        String url = jsonObject.optString("path");
                        if (url != null) {
                            mUrlSplash = url;
                            PreferencesUtils.put(KEY_SPLASH, url);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Exception e) {
                int i = 0;
            }
        });
    }


}
